package com.it.tydic.estate.module.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.common.util.CommonUtils;
import com.it.tydic.estate.common.util.InstaIdGenerator;
import com.it.tydic.estate.common.util.TimeUtil;
import com.it.tydic.estate.module.mapper.inner.TbWxglBmMapper;
import com.it.tydic.estate.module.model.inner.TbWxglBm;
import com.it.tydic.estate.module.service.WxglBmService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 维修管理 实现
 * Created by jinghongtao on 2020/3/3.
 */
@Transactional
@Service
public class WxglBmServiceImpl implements WxglBmService{

    @Resource
    private TbWxglBmMapper tbWxglBmMapper;
    /****
     * 分页查询 数据列表
     * @param record
     * @return
     */
    @Override
    public Result queryByPage(Map<String,Object> record) {
        Map<String,Object> map = new HashMap<>();
        CommonUtils.pageProcess(record);//验证处理分页
        record.put("zfbj","0");//查未删的数据
        Page<TbWxglBm> xiaoquguanlis = tbWxglBmMapper.selectLists(record);
        List<TbWxglBm> result = xiaoquguanlis.getResult();
        map.put("result",result);
        map.put("totle",xiaoquguanlis.getTotal());
        return Result.success(map);
    }
    /**
     * 查询 所有部门
     * @param
     * @return
     */
    @Override
    public  Result getAllData() {
        List<TbWxglBm> bmList = tbWxglBmMapper.selectAll();
        return Result.success(bmList);
    }
    /**
     * 根据id 查询单条记录
     * @param id
     * @return
     */
    @Override
    public  Result selectByPrimaryKey(String id) {
        if("".equals(id) || id.length()==0){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }else {
            TbWxglBm tbWxglBm = tbWxglBmMapper.selectByPrimaryKey(id);
            return Result.success(tbWxglBm);
        }

    }
    /**
     * 根据id 删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(String id){
        if("".equals(id) || id.length()==0){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }else {
            int i = tbWxglBmMapper.deleteByPrimaryKey(id);
            if(0 <i){
                return   Result.success(CodeMsg.SUCCESS);
            }else {
                return   Result.error(CodeMsg.DELETE_ERROR);
            }
        }

    }
    /**
     * 根据id 逻辑删除
     * @param id
     * @return
     */
    @Override
    public  Result deleteUp(String id){
        if(null==id || id.length()==0){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }else {
            int i = tbWxglBmMapper.deleteUp(id);
            if(1 == i){
                return   Result.success(CodeMsg.SUCCESS);
            }else {
                return   Result.error(CodeMsg.DELETE_ERROR);
            }
        }

    }
    /**
     *  批量逻辑删除
     * @param ids
     * @return
     */
    @Override
    public Result deleteByIds(String[] ids){
        if (ids.length == 0) {
            return  Result.error(CodeMsg.EMPTY_PARAM_ERROR,null);
        }
        int result = tbWxglBmMapper.deleteByList(ids);
        if (result == ids.length) {
            return  Result.success(CodeMsg.SUCCESS);
        }
        return Result.error(CodeMsg.DELETE_ERROR, null);
    }
    /**
     * 根据id 更新
     * @param record
     * @return
     */
    @Override
    public  Result updateData(TbWxglBm record){
        //record.setBdczsj(new Date());//报单操作时间
        //record.setBdczry("");//报单操作人员
        int i = tbWxglBmMapper.updateByPrimaryKeySelective(record);
        if(1 == i){
            return  Result.success(CodeMsg.SUCCESS);
        }else {
            return Result.error(CodeMsg.UPDATE_ERROR,null);
        }
    }
    /**
     * 保存
     * @param record
     * @return
     */
    @Override
    public Result saveDate(TbWxglBm record){
        if(null == (record.getBmid()) || "".equals(record.getBmid())){
            long id = new InstaIdGenerator().nextId(TimeUtil.timeGen());
            record.setBmid(String.valueOf(id));
            record.setCzsj(new Date());//报单操作时间
            //record.setCzr("");//报单操作人员
            record.setZfbj("0");//删除标记：0正常，1 删除
            int i = tbWxglBmMapper.insertSelective(record);
            if(1 == i){
                return  Result.success(record);
            }else {
                return  Result.error(CodeMsg.INSERT_ERROR,null);
            }
        }else {
            return  Result.error(CodeMsg.INSERT_ERROR,null);
        }

    }

}
